<?php
class Marcas_model extends CI_Model {
    public function __construct() {
        $this->load->database();
    }
    
    public function get_marcas() {
        $query = $this->db->query("SELECT *
                                    FROM
                                        marcas
                                    WHERE
                                        habilitado = 1
                                    ORDER BY
                                        nombre");
        return $query->result_array();
    }
    
    public function existe_marca_por_nombre($nombre) {
        $query = $this->db->query("SELECT count(*) as cant
                                    FROM
                                        marcas
                                    WHERE
                                        nombre = '$nombre'");
        $res = $query->row_array();
        
        if($res['cant'] == 0) {
            return false;
        } else {
            return true;
        }
    }
    
    public function set_marca($datos) {
        $this->db->insert('marcas', $datos);
    }
    
    public function get_marca_por_id($id) {
        $query = $this->db->query("SELECT *
                                    FROM
                                        marcas
                                    WHERE
                                        idMarca = $id");
        return $query->row_array();
    }
    
    public function update($datos, $id) {
        $id = array(
            'idMarca' => $id
        );
        
        $this->db->update('marcas', $datos, $id);
    }
    
    public function get_cantidad_de_marcas() {
        $query = $this->db->query("SELECT COUNT(*) AS cant
                                    FROM
                                        marcas");
        return $query->row_array();
    }
}
?>
